******************************************************
* This program creates data to construct RD graphs for various outcomes/ages/cutyrs/bin sizes following approved format for disclosure.

version 14
clear all
pause on
set more off
capture macro drop _all

global stata_code = "./stata_code"
*global stata_code = "C:\Users\Alexander.Smith\Documents\GitHub\eitc\taxtax"
*global clean_data = "Z:\research_smith_alex\eitc\data\tax\synth"
do ${stata_code}/taxtax_define_specs.do   // This defines various elements of different specfications so they can be called easily below.
*quietly do ${utility}/alex_utility14.do  // This sets up regrunner program.


global full_y = "male white_non_hisp black hisp tyrc_maxage parent_i_1040 i_married0 tyrc_pragiz i_pov0 tyrc_prdtax_taxsimL tyrc_prdtax_taxsimL0" //"male parent_wagez i_pov0 parent_i_1040 i_married0 tyrc_prdeitc tyrc_prdtax_taxsimL"
global outcomes = "parent_wagez i_pov0 i_married0 " //"i_pov0 i_married0"
global graph_samps = "k1elig"
global graph_predsamps = "predL12"
global graph_tyrcs = "1979_1991"

global t_neg12 = "& (t==-1 | t==-2)"
global t_3_4 = "& (t==3 | t==4)"

foreach y of global full_y {
	global ts_`y' = "neg12"
}

foreach y of global outcomes {
	global ts_`y' = "neg12 3_4"
}


global w = 30   //WINDOW SIZE (+/-)
global g = "g" //Use gcollapse?

if "${g}"=="g" ssc install gtools //Efficient collapse tool

use if ((t==-1 & (taxyr_cut==1980 | taxyr_cut==1985 | taxyr_cut==1990)) | (t==-2 & (taxyr_cut==1981 | taxyr_cut==1986 | taxyr_cut==1991)) ///
				| (t==3 & (taxyr_cut==1981 | taxyr_cut==1986 | taxyr_cut==1991)) | (t==4 & (taxyr_cut==1980 | taxyr_cut==1985 | taxyr_cut==1990))) ///
				${r_elig} & inrange(z,-${w}-.01,${w}-.01) ///
  		using ${clean_data}/taxtax_parents_mech1_clean, clear

capture replace tyrc_prdtax_taxsimL0 = 0 if z>=0

*save C:\Users\Alexander.Smith\Documents\temp0, replace

*use C:\Users\Alexander.Smith\Documents\temp0, clear

/* For Alex to run on fake data
capture noisily drop y* n* bin_z i_obstaxyr
gen i_obstaxyr = 1
gen tyrc_iprnoeitc5 =tyrc_iprnoeitc
gen tyrc_ipreitcanyb0 = tyrc_ipreitcany
gen birth_order0 = birth_order
foreach x in i_interest i_invest i_pov3 i_epov3 i_midclass3 tyrc_prdeitc_taxsim tyrc_prdtax_taxsim {
	gen `x' = 1
}
*/

foreach b in 2 {  //Bin Size
	local bins = ""
	capture drop bin_z
	gen bin_z=z-mod(z,`b')+`b'/2
	gen bin_z0=z-mod(z,`b')

	local i = 0
	label define SEQ 0 "BLANK"

	foreach y of global full_y {
		disp ""
		disp "**************************************"
		disp "**************Y = `y'*****************"
		foreach s1 of global graph_samps {
			disp ""
			disp "***********S1 = `s1'"
			foreach s2 of global ts_`y' {
				disp ""
				disp "********S2 = `s2'"
				foreach s3 of global graph_tyrcs {
					disp ""
					disp "********S3 = `s3'"
					foreach s4 in dropany_miss {
						disp ""
						disp "*****S4 = `s4'"
						foreach s5 of global graph_predsamps {
							disp ""
							disp "*S5 = `s5'"
							local i = `i'+1
							label define SEQ `i' "`s1' `s2' `s3' `s4' `s5' `y' ", add
							disp "`i' --- `s1' `s2' `s3' `s4' `s5' `y' "
							qui gen y_`i' = `y' ///
								if ${tyrcs_`s3'} ///
									${r_`s1'} ///
									${t_`s2'} ///
									${sc_`s4'} ///
									${rp_`s5'}

							qui gen n_`i' = 1 ///
										if ${tyrcs_`s3'} ///
											${r_`s1'} ///
											${t_`s2'} ///
											${sc_`s4'} ///
											${rp_`s5'}
						}
					}
				}
			}
		}
	}
}

${g}collapse (mean) y_* (sum) n_* , by(bin_z bin_z0)
reshape long y_ n_ , i(bin_z) j(seqcode)

label values seqcode SEQ
decode seqcode , gen(seq)
*tab seq, m
split seq, gen(s)

rename s6 outcome
rename y_ mean
rename n_ obs
label values seqcode
format seqcode %9.0g

order outcome s1-s5 bin_z0 mean obs seqcode seq
rename s1 sample
rename s2 t
rename s3 taxyrcuts
rename s4 sch_cuts
rename s5 predsamps

sort seqcode bin_z0

**********************************
**********DIAGNOSTICS:
list outcome sample predsamps if bin_z0==0

tab outcome sample, m
tab outcome predsamps, m
summ obs, detail


disp "MIN CELL SIZE: ",r(min)
**********************************

save ${results}/mechrd_graph, replace
